// EditPage.cpp

#include "StdAfx.h"

#include "EditPage.h"
#include "resource.h"

#include "Common/System/Shell.h"
#include "HelpUtils.h"
#include "Common/Lang/LangUtils.h"
#include "Common/Registry/RegistryUtils.h"

using namespace Common;

static Lang::CIDLangPair kIDLangPairs[] =
{
  { IDC_EDIT_STATIC_EDITOR, 0x03010201},
  { IDC_EDIT_STATIC_DIFF, 0x03010202}
};

static LPCWSTR kEditTopic = _T("FM/options.htm#editor");

LRESULT CEditPage::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
  Lang::LangSetDlgItemsText(HWND(*this), kIDLangPairs, sizeof(kIDLangPairs) / sizeof(kIDLangPairs[0]));

  
  _editor.Attach(GetDlgItem(IDC_EDIT_EDIT_EDITOR));
  _diff.Attach(GetDlgItem(IDC_EDIT_EDIT_DIFF));
  
  {
    CString path;
    Registry::ReadRegEditor(path);
    _editor.SetWindowText(path);
  }
  {
    CString path;
    Registry::ReadRegDiff(path);
    _diff.SetWindowText(path);
  }
  return TRUE;
}

int CEditPage::OnApply()
{
  {
    CString path;
    _editor.GetWindowText(path);
    Registry::SaveRegEditor(path);
  }
  {
    CString path;
    _diff.GetWindowText(path);
    Registry::SaveRegDiff(path);
  }
  return TRUE;
}

void CEditPage::OnHelp()
{
  ShowHelpWindow(NULL, kEditTopic);
}

static void Edit_BrowseForFile(CEdit &edit, HWND hwnd)
{
  CString path;
  edit.GetWindowText(path);
  CFileDialog dlg(TRUE, _T("exe"), (path.GetLength() ? (LPCTSTR)path : NULL), OFN_EXPLORER | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, _T("Executable(*.exe)\0*.exe\0All Files(*.*)\0*.*\0"), hwnd);
  if(IDOK == dlg.DoModal())
  {
	  edit.SetWindowText(dlg.m_szFileName);
  }
}


LRESULT CEditPage::OnSetPathEditor(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	Edit_BrowseForFile(_editor, *this);
	return 0;
}

LRESULT CEditPage::OnSetPathDiff(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	Edit_BrowseForFile(_diff, *this);
	return 0;
}

LRESULT CEditPage::OnEditPath(WORD wNotifyCode, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (wNotifyCode == EN_CHANGE)
	{
		SetModified();
	}
	return TRUE;
}
